--[[
	获取赠送金额
]]
local filename = "getgivemoney.lua"
local cluster = require "skynet.cluster"
local skynet = require "skynet"
local queue = require "skynet.queue"
local filelog = require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}

local cs = queue()

--
--领取赠送
--
local function getgivemoney(rid, msgbody)
	local keyid = tostring(msgbody.keyid)
	local charge_type = EDiamondChange.GIVEMONEY_PLAYER
	local newstate = EGiveMoneySate.EGIVEMONEY_STATE_HAD_DRAW
	
	--修改状态
	local result = skynet.call(".givesystem", "lua", "update_givemoney_state", keyid, newstate)
	if not result or result.retcode then
		return result or {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "领取失败"}
	end
	
	--修改金额
	local value = tonumber(result.give_value)
	local give_type = tonumber(result.give_type)
	if give_type == EGiveMoneyType.EGIVEMONEY_TYPE_VIP_CHARGE 
	or give_type == EGiveMoneyType.EGIVEMONEY_TYPE_GM_RECHARGE
	or give_type == EGiveMoneyType.EGIVEMONEY_TYPE_SYS_RECHARGE then   --VIP充值或补单
		charge_type = EDiamondChange.GIVEMONEY_CHARGE
	elseif give_type == EGiveMoneyType.EGIVEMONEY_TYPE_CP_AWARD then	--彩票中奖
		charge_type = EDiamondChange.GIVEMONEY_CPAWARD
	elseif give_type == EGiveMoneyType.EGIVEMONEY_TYPE_CP_BACK then		--彩票退回
		charge_type = EDiamondChange.GIVEMONEY_CPBACK
	elseif give_type == EGiveMoneyType.EGIVEMONEY_TYPE_SYS_AWARD then	--活动奖励
		charge_type = EDiamondChange.GIVEMONEY_ACTIVITY
	end
	service:charge_diamond(value, {change_type = charge_type, tradeid = result.tradeid}, "give")
	
	--保存充值记录
	if charge_type == EDiamondChange.GIVEMONEY_CHARGE then
		skynet.send(".roletablestate", "lua", "write_chargerecord", rid, value)
	end
	
	--彩票中奖，更新打码值
	if give_type == EGiveMoneyType.EGIVEMONEY_TYPE_CP_AWARD then
		local updatelist = {}
		service.money.total_chipvalue = service.money.total_chipvalue + value
		service.money.base_chipvalue = service.money.base_chipvalue - value
		if service.money.base_chipvalue <= 0 then
			service.money.base_chipvalue = 0
		end
		if service.money.limit_chipvalue > 0 then
			service.money.phase_chipvalue = service.money.phase_chipvalue + value
			updatelist.phase_chipvalue = service.money.phase_chipvalue
		end
		updatelist.base_chipvalue = service.money.base_chipvalue
		updatelist.total_chipvalue = service.money.total_chipvalue
		skynet.pcall(cluster.send, "datanode", ".router", "update_rk", rid, "rs_money", 0, updatelist)
		
		return {}
	end
	
	--赠送或充值，改变打码值
	local base_chipvalue = service.money.base_chipvalue
	if give_type == EGiveMoneyType.EGIVEMONEY_TYPE_SYS_AWARD then
		service.money.limit_chipvalue = service.money.limit_chipvalue + value
		service.money.base_chipvalue = service.money.base_chipvalue + 3 * value	--系统奖励，增加3倍打码值
	elseif charge_type == EDiamondChange.GIVEMONEY_CHARGE 
	or charge_type == EDiamondChange.GIVEMONEY_PLAYER then
		service.money.limit_chipvalue = service.money.limit_chipvalue + value
		service.money.base_chipvalue = service.money.base_chipvalue + value
	end
	if base_chipvalue ~= service.money.base_chipvalue then
		local updatelist = { 
			base_chipvalue = service.money.base_chipvalue, 
			limit_chipvalue = service.money.limit_chipvalue,
		}
		skynet.pcall(cluster.send, "datanode", ".router", "update_rk", rid, "rs_money", 0, updatelist)
	end

	return {}
end

-- 处理玩家领取赠送
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
	
	return cs(getgivemoney, rid, msgbody)
end

return CMD
